<?php
namespace app\modules\cn\models;

use yii;
use yii\db\ActiveRecord;

class MockPublic extends ActiveRecord
{

    public static function tableName()
    {
        return '{{%mock_public}}';
    }

    /**
     * @Title  [修改模考的状态]
     * @Author Yoyo
     * @Date   2023-04-28
     */
    public function MockStatus()
    {
        $uid = \Yii::$app->session->get('uid');
        $mockData = $uid==false?array(): MockPublic::find()->asArray()->where('uid=' . $uid . ' and status=0')->orderBy('id desc')->all();//超过半小时的中断就变成未完成
        foreach ($mockData as $k => $v) {
            $detailData = MockDetail::find()->asArray()->where('pub_id=' . $v['id'] . ' and uid=' . $uid)->orderby('id desc')->one();
            if (($detailData != false && (time() - $detailData[0]['createTime']) > 1800) || ($detailData == false && (time() - $v['startTime'] > 1800))) { //这里有中断的情况,最后一题距离现在的时间是都超过半小时
                $endTime = $detailData == false ? ($v['startTime'] + 1800) : ($detailData[0]['createTime'] + 1800);
                MockPublic::updateAll(['status' => 2, 'endTime' => $endTime], 'id=' . $v['id']);
            }
        }
    }

    public function getResultid($status, $type, $cate_id)
    {
        $uid = \Yii::$app->session->get('uid');
        if ($status == false) {
            $re = \Yii::$app->db->createCommand()->insert("{{%mock_public}}", ['type' => $type, 'startTime' => time(), 'createTime' => time(), 'uid' => $uid, 'mockcate_id' => $cate_id])->execute();
            $resultid = MockPublic::find()->asArray()->where('uid=' . $uid)->orderBy('id desc')->one()['id'];
        } else {
            $resultid = MockPublic::find()->asArray()->where('uid=' . $uid . ' and type=' . $type . ' and mockcate_id=' . $cate_id)->orderBy('id desc')->one()['id'];
        }
        return $resultid;
    }

    public function getRecord($type, $page)
    {
        $uid = \Yii::$app->session->get('uid');
        $offset = ($page - 1) * 10;
        $sql = "select * from x2_mock_public where id in(select max(id) from x2_mock_public group by type,mockcate_id) and uid=$uid and type=$type order by id limit $offset,10 ";
        $data = Yii::$app->db->createCommand($sql)->queryAll();//获取了记录
        $data = self::getStatus($data);
        foreach ($data as $k => $v) {
            if ($v['status'] == 1) {
                $time = $v['endTime'] - $v['startTime'];
            } else {
                $lastAnswer = MockAnswer::find()->asArray()->where('pub_id=' . $v['id'])->orderBy('id desc')->one();
                $time = 0;
                if ($lastAnswer != false) {
                    $time = $lastAnswer['createtime'] - $v['startTime'];
                }
            }
            $data[$k]['doWriteCount'] = MockAnswer::find()->asArray()->where('pub_id=' . $v['id'] . ' and typeid=3 and answer!=""')->count();
            $data[$k]['doReadCount'] = MockAnswer::find()->asArray()->where('pub_id=' . $v['id'] . ' and typeid=2 and answer!=""')->count();
            $data[$k]['doListenCount'] = MockAnswer::find()->asArray()->where('pub_id=' . $v['id'] . ' and typeid=1 and answer!=""')->count();
            $data[$k]['readCount'] = 40;
            $data[$k]['writeCount'] = 2;
            $data[$k]['listenCount'] = 40;
            $data[$k]['mockTime'] = floor($time / 60) . 'm' . ($time % 60) . 's';//做题时间
            $data[$k]['name'] = '雅思机考' . ($type == 1 ? '听力' : ($type == 5 ? '全套' : '阅读')) . '模拟卷' . Yii::$app->params['noToStr'][$v['mockcate_id']];
        }
        return $data;
    }

    public function getStatus($data)
    {
        $uid = \Yii::$app->session->get('uid');
        foreach ($data as $k => $v) {
            $data[$k]['isOver'] = $data[$k]['continue'] = $data[$k]['time'] = 0;
            $question = MockCate::find()->asArray()->where('id=' . $v['mockcate_id'])->one();
            if ($v['type'] == 1) {//听力
                $pid = $question['listen'];
            } elseif ($v['type'] == 2) {//阅读
                $pid = $question['read'];
            } else {
                $pid = $question['listen'] . ',' . $question['read'] . ',' . $question['write'];
            }
            $data[$k]['startId'] = explode(',', $pid)[0];
            if ($v['status'] == false) {//做题中
                $detailData = MockDetail::find()->asArray()->where('pub_id=' . $v['id'] . ' and uid=' . $uid)->orderby('id desc')->all();
                $data[$k]['continue'] = 1;//中断
                if ($detailData == false) {
                    $data[$k]['time'] = 1800;
                } else {
                    $data[$k]['time'] = 1800 - (time() - $detailData[0]['createTime']);
                    $data[$k]['startId'] = array_values(array_diff(explode(',', $pid), array_column($detailData, 'content_id')))[0];//id
                }
            } elseif ($v['status'] == 1) {
                $data[$k]['isOver'] = 1;
            } elseif ($v['status'] == 2) {//未完成
                $data[$k]['continue'] = 2;
                $data[$k]['isOver'] = 1;//中断
            }
        }
        return $data;
    }

}
